/*==============================================
 * Task2a: 3D models
 *
 * Written by <YOUR NAME HERE>
 *
 *==============================================*/

#include "GameUtils.h"
#include "GameEngine.h"
#include "Fail.h"
#include "XMesh.h"


/** Task 2a: 3D models.
*/
class CTask2a: public CGameEngine
{
	virtual bool Startup();
	virtual void Update(float dt);
	virtual void Draw(float dt);
	virtual void Shutdown();
protected:
	CXMesh* mpTeapot;
	CXMesh* mpCrate;
	CXMesh* mpGround;
};


bool CTask2a::Startup()
{
	if (!StartupWinD3D(640,480,false))
		return false;

	mpTeapot=new CXMesh(mpDevice,"media/teapot.x");
	mpCrate=new CXMesh(mpDevice,"media/crate.x");
	mpGround=new CXMesh(mpDevice,"media/checker.x");

	// this should setup camera/lights & all the rest
	SetupDefaultD3DOptions(mpDevice,true);
	return true;
}
void CTask2a::Update(float dt)
{
	if (KeyPress(VK_F10))
		CloseWin();

	ShowFrameRate();	// show the frame rate
}
void CTask2a::Draw(float dt)
{
    // Clear the backbuffer to a grey color
    mpDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, WHITE_COL*0.5f, 1.0f, 0 );
	mpDevice->BeginScene();

	// the teapot is spinning a lot
	// (get time is a function in CGameWindow which returns the current time)
	D3DXMATRIX world,rot,trans;
	D3DXMatrixRotationYawPitchRoll(&rot,GetTime(),GetTime(),GetTime());
	D3DXMatrixTranslation(&trans,0,1,0);
	world=rot*trans;	// multiple the simple way
	mpDevice->SetTransform(D3DTS_WORLD,&world);
	mpTeapot->Draw();	// draws based upon the current world matrix

	// two cartes: one is just translated
	D3DXMatrixTranslation(&trans,2,0,0);
	mpCrate->Draw(trans);	// draws using this matrix

	// the other is translated and scaled:
	mpCrate->Draw(D3DXVECTOR3(-2,0,0),2.0f);	// draws at location with scale

	// ground is at 0,0,0
	mpGround->Draw(D3DXVECTOR3(0,0,0));	// draw at this location with this scale

	mpDevice->EndScene();
	// Present the backbuffer contents to the display
    mpDevice->Present( NULL, NULL, NULL, NULL );
}
void CTask2a::Shutdown()
{
	SAFE_DELETE(mpTeapot);
	SAFE_DELETE(mpCrate);
	SAFE_DELETE(mpGround);
	ShutdownD3D();
}


int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
	ENABLE_LEAK_DETECTION();
	CTask2a eng;
	return eng.Go(hInstance);	// runs everything
}
